package com.material.vtool.controller;

import com.google.common.collect.Maps;
import com.material.vtool.entity.combinedEntity.CombinedTeamMaterialEntity;
import com.material.vtool.entity.combinedEntity.PageResult;
import com.material.vtool.entity.reqEntity.MaterialCommentReqBean;
import com.material.vtool.entity.reqEntity.TeamMaterialPageBean;
import com.material.vtool.entity.reqEntity.TopTeamMaterialReqBean;
import com.material.vtool.entity.respEntity.RespData;
import com.material.vtool.entity.respEntity.ReturnCode;
import com.material.vtool.service.TeamMaterialService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.POST;
import java.util.HashMap;
import java.util.List;

/**
 * 团队素材
 */
@RestController
@RequestMapping("/teamMaterial")
public class TeamMaterialController {
    @Autowired
    private TeamMaterialService teamMaterialService;

    /**
     * 按标签获取团队素材列表
     * @param teamMaterialPageBean 团队素材分页实体类
     * @return
     */
    @PostMapping(value = "/list", produces = "application/json;charset=UTF-8")
    public RespData list(@RequestBody TeamMaterialPageBean teamMaterialPageBean) {
        if (teamMaterialPageBean == null || teamMaterialPageBean.getTeamId() == null || teamMaterialPageBean.getLabelTypeId() == null) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        Integer teamId = teamMaterialPageBean.getTeamId();
        Integer labelTypeId = teamMaterialPageBean.getLabelTypeId();

        Integer pageNum = teamMaterialPageBean.getPageNum();
        Integer pageSize = teamMaterialPageBean.getPageSize();

        if (pageNum == null || pageNum.intValue() <= 0) {
            pageNum = Integer.valueOf(1);
        }

        if (pageSize == null || pageNum.intValue() <= 0) {
            pageSize = Integer.valueOf(10);
        }

        List<CombinedTeamMaterialEntity> materialList = teamMaterialService.getTeamMaterialList(pageNum, pageSize, teamId, labelTypeId);

        HashMap<Object, Object> resultMap = Maps.newHashMapWithExpectedSize(10);
        if (labelTypeId.intValue() >= 0) {
            int totalRecord = 0;// 总记录数
            int totalPage = 0;// 总页数
            totalRecord = teamMaterialService.getTeamMaterialListTotal(teamId, labelTypeId);
            totalPage = totalRecord % pageSize.intValue() == 0 ? totalRecord / pageSize.intValue() : (totalRecord / pageSize.intValue()) + 1;
            PageResult pageResult = new PageResult(Integer.valueOf(totalRecord), pageSize, pageNum, Integer.valueOf(totalPage));
            resultMap.put("pageResult", pageResult);
        }
        resultMap.put("materialList", materialList);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(resultMap);
        return respData;
    }

    /**
     * 删除团队素材
     * @param id 团队素材ID
     * @return
     */
    @PostMapping(value = "/deleteTeamMaterial/{id}", produces = "application/json;charset=UTF-8")
    public RespData deleteTeamMaterial(@PathVariable("id") Integer id) {
        if (id == null || id.intValue() <= 0) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        teamMaterialService.deleteTeamMaterialById(id);

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 团队素材置顶
     * @param topTeamMaterialReqBean 请求实体
     * @return
     */
    @PostMapping(value = "/topTeamMaterial", produces = "application/json;charset=UTF-8")
    public RespData topTeamMaterial(@RequestBody TopTeamMaterialReqBean topTeamMaterialReqBean) {
        if (topTeamMaterialReqBean == null || topTeamMaterialReqBean.getId() == null || topTeamMaterialReqBean.getId().intValue() <= 0
            || topTeamMaterialReqBean.getTeamId() == null || topTeamMaterialReqBean.getTeamId().intValue() <= 0) {
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        teamMaterialService.updateTeamMaterialTopById(topTeamMaterialReqBean.getId(), topTeamMaterialReqBean.getTeamId());

        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }
}
